package com.newsinfo.controller;

import com.newsinfo.pojo.Article;
import com.newsinfo.pojo.PageBean;
import com.newsinfo.pojo.Result;
import com.newsinfo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * 新增文章
     * @param article
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated Article article){
        return articleService.add(article);
    }

    /**
     * 分页查询文章列表
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param state
     * @return
     */
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ){
        PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state);
        return Result.success(pb);
    }

    /**
     * 更新文章
     * @param article
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated Article article){
        return articleService.update(article);
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result<Article> detail(Integer id){
        return articleService.detail(id);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(Integer id){
        return articleService.delete(id);
    }
}
